// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property, executeInEditMode } = cc._decorator;

@ccclass
@executeInEditMode
export default class StencilRender extends cc.Component {
    @property({
        min: 1,
        max: 255,
        step: 1,
    })
    stencilRef: number = 1;

    onLoad() {
        let sprite = this.node.getComponent(cc.RenderComponent);
        let gfx = cc["gfx"];
        let mat = sprite.getMaterial(0);
        mat.setStencil(
            gfx.STENCIL_ENABLE,
            gfx.DS_FUNC_EQUAL,
            this.stencilRef,
            1,
            gfx.STENCIL_OP_KEEP,
            gfx.STENCIL_OP_KEEP,
            gfx.STENCIL_OP_KEEP,
            1,
            0
        );
    }
    
    protected onDisable(): void {
        let render = this.node.getComponent(cc.RenderComponent);
        let gfx = cc["gfx"];
        let mat = render.getMaterial(0);
        mat.setStencilEnabled(gfx.STENCIL_DISABLE, 0);
    }
}